package com.smartEdu.service.impl.app;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.smartEdu.commons.CMap;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.ReqMap;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.HomeWorkDao;

import com.smartEdu.dao.HomeworkSubmitDao;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.model.CompletedModel;
import com.smartEdu.model.HomeWorkModel;
import com.smartEdu.model.HomeworkSingleSubmitModel;
import com.smartEdu.model.HomeworkSubmitModel;
import com.smartEdu.model.HomeWorkModel2;
import com.smartEdu.service.HomeWorkService;
import com.smartEdu.utils.JsonUtil;



@Service(value="homeWorkServiceImpl")
public class HomeWorkServiceImpl extends BaseServiceImpl implements HomeWorkService{
	
	@Autowired
	@Qualifier(value="homeWorkDaoImpl")
	private HomeWorkDao homeWorkDao;
	

	@Autowired
	@Qualifier("homeworkSubmitDaoImpl")
	private HomeworkSubmitDao homeworkSubmitDao;

	@Override
	public void GetWarm(Session sess) {
		Map<String, Object> jsonmap=null;
		try{
			jsonmap=JsonUtil.toMap(sess.gets(Tag_Tips.REQUEST));
			jsonmap.get("id").toString();
		}
		catch(JsonException e){
			
		}
	}

	@Override
	public void homeWorkDetail(Session sess) {
		List<HomeWorkModel> models=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			models=homeWorkDao.getHomeWorkDetail(map.gets("pk"));
			sess.put("hms", models);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
			
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("HomeWork业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("HomeWork业务流程出错");
		}
		finally {
			map=null;
		}
	}

	@Override
	public void homeWork2(Session sess){
		List<HomeWorkModel2> models=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			String cstable = "course_student_"+Tag_Tips.ACADEMICVALUE;
			ReqMap reqMap = new ReqMap();
			reqMap.put("id", map.gets("id"));
			reqMap.put("cstable", cstable);
			models=homeWorkDao.getHomeWork2(reqMap);
			sess.put("hms", models);
			sess.put("key", "value");
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("HomeWork业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("HomeWork业务流程出错");
		}
		finally {
			map=null;
		}
	}
	
	@Override
	public void homeworkSubmit(Session sess) {
		// TODO Auto-generated method stub
	    HomeworkSubmitModel model=null;
		CMap map=null;
		String test=null;
		try{
			map=SetToMap(sess);
			model=homeworkSubmitDao.getHomeworkCompleted(map.getVals());
			if(model==null){
				model=new HomeworkSubmitModel();
				sess.put("num",map.geti("num"));
				model.setCompleted(JsonUtil.toJson(map.geto("completed")));
			}else{
				List<HomeworkSingleSubmitModel>completedList=(List<HomeworkSingleSubmitModel>) model.getCompleted();
				List<HomeworkSingleSubmitModel>submitList=(List<HomeworkSingleSubmitModel>) map.geto("completed");
				completedList.addAll(submitList);
				sess.put("num",completedList.size());
				model.setCompleted(JsonUtil.toJson(completedList));
			}
		
			model.setPk(map.gets("pk"));
			model.setSid(map.gets("sid"));
			homeworkSubmitDao.addOrUpdateHomeworkCompleted(model);
		}catch(JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("HomeworkSubmit业务流程出错");
		}catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("HomeWorkSubmit业务流程出错");
		}finally {
			map=null;
		}
	}

	@Override
	public void ShowHomeWork(Session sess) {
		
	}
	
	public static List<HomeworkSingleSubmitModel> paixv(List<HomeworkSingleSubmitModel> list){
	        int swap;
	        String str=null;
	        for(int i=0;i<list.size()-1;i++){
	        	for(int j=i+1;j<list.size();j++){
	        		if(list.get(i).getQid()>list.get(j).getQid()){
	        			swap=list.get(j).getQid();
	        			list.get(j).setQid(list.get(i).getQid());
	        			list.get(i).setQid(swap);
	        			
	        			swap=list.get(j).getQtype();
	        			list.get(j).setQtype(list.get(i).getQtype());
	        			list.get(i).setQtype(swap);
	        			
	        			swap=list.get(j).getRight_wrong();
	        			list.get(j).setRight_wrong(list.get(i).getRight_wrong());
	        			list.get(i).setRight_wrong(swap);
	        			
	        			str=list.get(j).getAnswer();
	        			list.get(j).setAnswer(list.get(i).getAnswer());
	        			list.get(i).setAnswer(str);
	        			
	        		}
	        	}
	        }
	        
		return list;
	}
	
}
